package com.jerry.walking.module.activityPage.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jerry.walking.R;
import com.jerry.walking.base.BaseViewHolder;
import com.jerry.walking.base.MyBaseAdapter;
import com.jerry.walking.module.activityPage.activity.TravelDetailActivity;
import com.jerry.walking.module.activityPage.bean.Lore;
import com.jerry.walking.utils.DateUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.Date;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2016/5/10 0010.
 */
public class ActivityItemAdapter extends MyBaseAdapter<Lore.ActivitiesBean>{

    private Context context;

    public ActivityItemAdapter(Context context, List<Lore.ActivitiesBean> list){
        super(context, list);
        this.context = context;
    }

    @Override
    public int getLayoutId(){
        return R.layout.item_home_list;
    }

    @Override
    public BaseViewHolder getViewHolder(){
        return new ActivityItemAdapter.ViewHolder();
    }

    @Override
    public void bindViews(View view, BaseViewHolder viewHolder){
        ViewHolder holder = (ViewHolder) viewHolder;
        Lore.ActivitiesBean item = getItem(holder.position);
        holder.tvTitle.setText(item.getTitle());
        holder.tvDestination.setText(item.getDestination());
        //        if(Long.parseLong(item.getStart_time()))
        if(!TextUtils.isEmpty(item.getPeriod_desc())){
            holder.tvStartTime.setText(item.getPeriod_desc());
        }else{
            /*SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
            Date date1 = new Date();
            String date = format.format(date1);
            holder.tvStartTime.setText(date);*/
            int start_time = item.getStart_time();
            String date = DateUtils.getDate(new Date(start_time));
            holder.tvStartTime.setText(date);
        }
        List<String> tag = item.getTag();
        TextView textView = null;
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
        holder.llTag.removeAllViews();
        for(int i = 0; i < tag.size(); i++){
            if(i != 0){
                params.setMargins(10, 0, 0, 0);
            }
            textView = new TextView(context);
            textView.setLayoutParams(params);
            textView.setBackgroundColor(context.getResources().getColor(R.color.color_bule));
            textView.setTextColor(context.getResources().getColor(R.color.color_wihte));
            textView.setText(tag.get(i));
            holder.llTag.addView(textView);

        }
        holder.tvDays.setText("行程" + item.getDays() + "天");
        holder.tvCost.setText("花费：" + item.getMin_cost());
        holder.tvLogoTitle.setText(item.getClub().getTitle());
        ImageLoader.getInstance().displayImage(item.getClub().getLogo(), holder.ivLogo);
        ImageLoader.getInstance().displayImage(item.getCover(), holder.ivCover);
        final int categoryId = item.getId();
        holder.llItem.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                Intent intent = new Intent(context, TravelDetailActivity.class);
                intent.putExtra("categoryId",categoryId);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }
        });
    }

    static class ViewHolder extends BaseViewHolder{
        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.tv_destination)
        TextView tvDestination;
        @BindView(R.id.tv_start_time)
        TextView tvStartTime;
        @BindView(R.id.tv_duration)
        TextView tvDays;

        @BindView(R.id.tv_cost)
        TextView tvCost;

        @BindView(R.id.tv_logo_title)
        TextView tvLogoTitle;
        @BindView(R.id.ll_tag)
        LinearLayout llTag;

        @BindView(R.id.iv_logo)
        ImageView ivLogo;

        @BindView(R.id.iv_cover)
        ImageView ivCover;
        @BindView(R.id.ll_item)
        LinearLayout llItem;
    }
}
